package com.xsherl.leetcode.solution;

import cn.hutool.core.lang.Assert;

public class IntToRoman {

    public static int[] radix = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
    public static String[] roman = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};

    /**
     * 将数字按罗马数字顺序由高到低依次转为对应的罗马数字，一直到转换为 0 为止
     */
    public String intToRoman(int num) {
        int index = radix.length - 1;
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            int m = radix[index];
            if (num >= m) {
                sb.append(roman[index]);
                num -= m;
            } else {
                index--;
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        int num = 1994;
        String roman = new IntToRoman().intToRoman(num);
        Assert.isTrue(roman.equals("MCMXCIV"));
    }

}
